package cn.com.callback

import cn.com.elite.EliteBean
import cn.com.elite.Util
import cn.com.kh.KHUtil
import cn.com.kh.LogToDB
import cn.com.kh.signUtil
import com.alibaba.fastjson.JSONArray
import com.alibaba.fastjson.JSONObject
import org.apache.commons.logging.Log
import org.apache.commons.logging.LogFactory
import org.springframework.jdbc.core.JdbcTemplate
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource

import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

/**
 * 移动工单处理API
 */

public class apiWoOperationEntrance implements ServiceBackInvoke {
    private static final Log log = LogFactory.getLog(apiWoOperationEntrance.class);
    private String charset = "utf-8"; //设置字符集
    private int timeout = 100000; //设置请求超时时间
    private int enableSSLflag = 0;
    private String insertString = "";

    public String doCallBack(EliteBean eliteBean, HttpServletRequest request, HttpServletResponse response) {

        long startTime = System.currentTimeMillis();
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json;charset=utf-8");
        JdbcTemplate jt = new JdbcTemplate(eliteBean.getDataSource());
        JSONObject result = new JSONObject();// 初始化返回参数
        String re_msg = "";
        String in_operation_type = "";
        JSONObject resultJson = null;

        //接收客户端调用参数
        String callJson = "";
        InputStreamReader reader = new InputStreamReader(request.getInputStream(), "UTF-8");
        BufferedReader iny = new BufferedReader(reader);
        String line;
        while ((line = iny.readLine()) != null) {
            callJson += line;
        }
        log.info("[调用请求信息]:" + callJson);

        try {
            callJson = URLDecoder.decode(callJson, "UTF-8");//转码
        } catch (Exception e) {
            log.warn("对入参报文尝试URLDecoder失败,使用原报文处理-可忽略" + e.getMessage());
        }

        try {
            String inJsonstr = filterJson(callJson);    //转译
            JSONObject inJson = JSONObject.parseObject(inJsonstr);  //解析Json
            log.info("[调用请求信息-解析Json后]:" + inJson);

            in_operation_type = inJson.getJSONObject("data").getString("operation_type");   //获取操作类型
            //工单操作类型，创建工单：CREATE 保存工单：SAVE 下送工单：SENDNS 退单：BACK 转派：TRANSFER 结案：CLOSE 作废：FAIL 撤单：ROBACK 重新打开：REOPEN 指派：ASSIGN
            String re_code = "";
            String dataSourceID = "dataSource";

            if (in_operation_type.equals("SAVE")) {
                result = SaveOrder(jt, inJson, eliteBean);
                re_msg = "工单保存成功";
                //result.put("code", 1);
                //判断是否属于新增工单
            } else if (in_operation_type.equals("CREATE")) {
                result = CreateOrder(jt, inJson, eliteBean);
                re_msg = "工单创建成功";
                //result.put("code", 1);
            } else if (in_operation_type.equals("SENDNS")) {
                result = SendOrder(jt, inJson, eliteBean);
                re_msg = "工单下送成功";
                //result.put("code", 1);
            } else if (in_operation_type.equals("BACK")) {
                result = BackOrder(jt, inJson, eliteBean);
                log.debug("退回返回结果：" + result);
                re_msg = "工单退回成功";
                //result.put("code", 1);
            } else if (in_operation_type.equals("CLOSE")) {
                result = CloseOrder(jt, inJson, eliteBean);
                re_msg = "工单结案成功";
                //result.put("code", 1);
            } else if (in_operation_type.equals("URGE")) {
                result = UrgeOrder(jt, inJson, eliteBean);
                re_msg = "工单催单成功";
                //result.put("code", 1);
            } else if (in_operation_type.equals("TAKE")) {
                result = TakeOrder(jt, inJson, eliteBean);
                re_msg = "工单接管成功";
                //result.put("code", 1);
            } else if (in_operation_type.equals("ASSIGN")) {
                result = ASSIGNOrder(jt, inJson, eliteBean);
                re_msg = "工单指派成功";
                //result.put("code", 1);
            } else if (in_operation_type.equals("test")) {
                lineTest(inJson, eliteBean);
            } else {
                result.put("msg", "当前工单没有该操作方法");
                result.put("code", "-1");
            }
            long endTime = System.currentTimeMillis();
            if (result.get("code") == "1") {
                result.put("msg", re_msg + " " + "处理完成、调用问题处理时长(毫秒):" + (endTime - startTime));
            }
        } catch (Exception e) {
            result.put("code", 1008);
            result.put("msg", e.getMessage());
        } finally {
            LogToDB.toDB(eliteBean, "dataSource", "工单内部接口-" + in_operation_type, "apiWoOperationEntrance", callJson, result.toString(), (System.currentTimeMillis() - startTime).toString(), re_msg);
        }
        return result.toString();
    }

    //保存工单
    public JSONObject SaveOrder(JdbcTemplate jt, JSONObject tabcValue, EliteBean eliteBean) {
        Map fieldValue = new HashMap<String, Object>();
        JSONObject result = new JSONObject();

        String in_s_objetive_guid = tabcValue.getJSONObject("data").getString("in_s_objetive_guid"); //当前objective_guid
        String in_s_elitetask_guid = tabcValue.getJSONObject("data").getString("in_s_elitetask_guid"); //当前objective_guid
        String in_r_group_id = tabcValue.getJSONObject("data").getString("in_r_group_id"); //下送组
        String in_r_staff_id = tabcValue.getJSONObject("data").getString("in_r_staff_id"); //下送人
        String in_workorderflowdetail = tabcValue.getJSONObject("data").getString("in_workorderflowdetail"); // 工单处理内容描述
        String map_id = tabcValue.getString("map_id"); //工单类型id
        String frame_id = tabcValue.getString("frame_id"); //工单模板id
        String step_id = tabcValue.getString("step_id"); //工单步骤id
        String y_createdby = tabcValue.getString("c_user_id"); //工单创建人
        String y_createdgroup = tabcValue.getString("c_group_id"); //创建组
        String customer_guid = tabcValue.getJSONObject("data").getString("customer_guid"); //当前customer_guid
        String workorderid = tabcValue.getJSONObject("data").getString("workorderid"); //工单流水ID
        String businessnumber = tabcValue.getJSONObject("data").getString("businessnumber"); //物业号
        String workordertype1 = tabcValue.getJSONObject("data").getString("workordertype1"); //受理渠道
        //String ys_code_id = inJson.getJSONObject("data").getString("ys_code_id"); //工单标识
        String ys_objtitle = tabcValue.getJSONObject("data").getString("ys_objtitle"); //工单标题
        String y_customerproblem = in_workorderflowdetail; //客户问题
        String tabcname = tabcValue.getString("tabcname"); //工单tabc表名

        JSONObject tabcJSON = tabcValue.getJSONObject("data").getJSONObject("business_info");//获取tabc字段信息
        fieldValue.putAll(tabcJSON);
        String fieldName = "";
        String valueString = "";
        String updateString = "";
        int i = 0;
        List<Object> valueList = new ArrayList<Object>();
        for (String key : fieldValue.keySet()) {
            log.debug("key:" + key);
            if (fieldName == "") {
                fieldName = key;
                valueString = "?";
                updateString = key + "=" + "?";
                insertString = key;
            } else {
                fieldName = fieldName + "," + key;
                valueString = valueString + ",?";
                updateString = updateString + "," + key + "=" + "?";
                insertString = insertString + "," + key;
            }
            //valueList.add(fieldValue.get(key));
            if (!String.valueOf(fieldValue.get(key)).equals("null")) {
                valueList.add(KHUtil.isNull(String.valueOf(fieldValue.get(key))) ? null : fieldValue.get(key));
            } else {
                valueList.add(fieldValue.get(key));
            }
            i++;
        }
        if (fieldName == "") {
            result.put("code", "-1");
            result.put("msg", "字段不能为空")
            return result;
        }

        List<Map<String, Object>> ordernums = jt.queryForList("select count(1) nums from objective where objective_guid='" + in_s_objetive_guid + "'");
        String ordernum = "0";
        for (Map<String, Object> numsorder : ordernums) {
            ordernum = numsorder.get("nums").toString();
        }
        log.debug("工单数量：" + ordernum);

        if (ordernum == "0") {
            log.debug("进入工单保存新增");
            //String Relation_ID = UUID.randomUUID().toString();
            String Relation_ID = in_s_objetive_guid;
            String[] data1 = new String[3];
            valueList.add(Relation_ID);
            valueString = valueString + ',?';
            String tabcsql = "insert into " + tabcname + "(" + fieldName + ",relation_id) value(" + valueString + ")";
            log.debug("参数值：" + valueList.toString());
            log.debug("tabcsql2：" + tabcsql);
            int tabcResule = jt.update(tabcsql, valueList.toArray());
            log.debug("tabcsql3：" + tabcsql);
            log.debug("tabcResule：" + tabcResule);

            if (tabcResule > 0) {
                data1[0] = "Insert Into Objective (Objective_Guid, Objectivetype_Id, Handleby_Id, Handlegroup_Id, Priority_Id, Expireddate, Comments,Customer_Guid, Firstcontactdate, Lastcontactdate, Objectivestatus, Iscontact, Trytimes, Successfulcontacttimes, Createdby, Createddate, Modifiedby, Modifieddate, Expecttradedate, Failreason, Quantity, Amount, Obstarttime, Obendtime, Firstcalldate, Failure_Attempt, Passflag, Localtrycount, Lastassigndate, Isanswer, Prepossibility, Expectstarttime, Workorderframe_Id, Category, Forecastdate, Workorderid, Objectivesubstatus, Map_Id, Curstep_Id_Map, Max_Attempt, Nocontacttry,businessnumber,workordertype1,DATATYPE) Values ('" + Relation_ID + "', '" + frame_id + "', '" + y_createdby + "', '" + y_createdgroup + "', 'NORMAL', now(), '" + in_workorderflowdetail + "','" + customer_guid + "', Null, Null, 'OPEN', '0', '0', '0', '" + y_createdby + "', now(), '" + y_createdby + "', now(), Null, '', '0', '0', Null, Null, Null, '0', '0', '0', Null, '0', '.00', Null, '" + map_id + "', 'CASE1', now(), '" + workorderid + "', 'OPEN ', '" + map_id + "', '" + step_id + "', '0', '0','" + businessnumber + "','" + workordertype1 + "','CUST')";
                log.debug("data1：" + data1[0]);
                data1[1] = "Insert Into Elitetask (Elitetask_Guid, Objective_Guid, Objectivetype_Id, Expectstarttime, Expectendtime, Priority, Handleby_Id, Handlegroup_Id, Createdby, Createddate, Modifiedby, Modifieddate, Comments, Customer_Guid, Customerlevel, Localtrycount, Curstepstatus, Curstep_Id, Stepforecastdate, Curstep_Id_Map, Map_Id, Objectivesubstatus, Elitetaskstatus, Obflag) Values (uuid(), '" + Relation_ID + "', '" + frame_id + "', now(), now(), 'NORMAL', '" + y_createdby + "', '" + y_createdgroup + "', '" + y_createdby + "', now(), '" + y_createdby + "', now(), '" + in_workorderflowdetail + "', '" + customer_guid + "', Null, '0', 'OPEN ', '" + step_id + "', now(), '" + step_id + "', '" + map_id + "', 'OPEN', Null, '0')";
                log.debug("data2：" + data1[1]);
                data1[2] = "Insert Into Workorderstep3 (Workorder_Guid, Workorderstep_Guid, oldHandleby_Id, oldHandlegroup_Id, Workorderstepstatus, Comments, Modifieddate, From_Stepidmap, Workstarttime, Workendtime) Values ('" + Relation_ID + "', uuid(), '" + y_createdby + "', '" + y_createdgroup + "', 'CLOSE', '" + in_workorderflowdetail + "', now(), '" + step_id + "', now(), now())";
                log.debug("data3：" + data1[2]);

                log.debug("判断处理工单状态进行下一步处理SQL:" + data1.toString());
                int[] dataresult1 = eliteBean.updateBatch(data1, "dataSource");
            }
        } else {
            log.debug("进入工单保存修改");
            String[] data1 = new String[3];

            eliteBean.update("insert into " + tabcname + "_his (hisguid," + insertString + ") select uuid()," + insertString + " from " + tabcname + " t where RELATION_ID='" + in_s_objetive_guid + "'", "dataSource");

            valueList.add(in_s_objetive_guid);
            String tabcsql = "update " + tabcname + " set " + updateString + " where relation_id=?";
            log.debug("参数值：" + valueList.toString());
            log.debug("tabc表sql:" + tabcsql);
            int tabcResule = jt.update(tabcsql, valueList.toArray());
            log.debug("修改数量:" + tabcResule);
            List selectLists = eliteBean.chaxun_sql("select * from " + tabcname + " where relation_id='" + in_s_objetive_guid + "'", "dataSource");
            log.debug(selectLists.toString());
            if (tabcResule > 0) {
                //data1[0] = "insert into tabcwo_business_info_his select uuid(),t.* from tabcwo_business_info t where RELATION_ID='" + in_s_objetive_guid + "'";
                data1[0] = "Update Objective Set Modifieddate = now(),handleby_id='" + y_createdby + "',handlegroup_id='" + y_createdgroup + "' Where Objective_Guid = '" + in_s_objetive_guid + "'";
                data1[1] = "Update Elitetask Set Modifieddate = now(),handleby_id='" + y_createdby + "',handlegroup_id='" + y_createdgroup + "' Where elitetask_guid = '" + in_s_elitetask_guid + "'";
                data1[2] = "Insert Into Workorderstep3 (Workorder_Guid, Workorderstep_Guid, oldHandleby_Id, oldHandlegroup_Id, Workorderstepstatus, Comments, Modifieddate, From_Stepidmap, Workstarttime, Workendtime) Values ('" + in_s_objetive_guid + "', uuid(), '" + y_createdby + "', '" + y_createdgroup + "', 'CLOSE', '" + in_workorderflowdetail + "', now(), '" + step_id + "', now(), now())";

                log.debug("判断处理工单状态进行下一步处理SQL:" + data1.toString());
                int[] dataresult1 = eliteBean.updateBatch(data1, "dataSource");
            }
        }

        result.put("code", "1");
        //result.put("msg", "保存成功")
        return result;

    }

    //创建工单
    public JSONObject CreateOrder(JdbcTemplate jt, JSONObject tabcValue, EliteBean eliteBean) {
        Map fieldValue = new HashMap<String, Object>();
        JSONObject result = new JSONObject();

        //String in_s_objetive_guid = tabcValue.getJSONObject("data").getString("in_s_objetive_guid"); //当前objective_guid
        String in_r_group_id = tabcValue.getString("in_r_group_id"); //下送组
        String in_r_staff_id = tabcValue.getString("in_r_staff_id"); //下送人
        String in_workorderflowdetail = tabcValue.getJSONObject("data").getString("in_workorderflowdetail"); // 工单处理内容描述
        String map_id = tabcValue.getString("map_id"); //工单类型id
        String frame_id = tabcValue.getString("frame_id"); //工单模板id
        String step_id = tabcValue.getString("step_id"); //工单步骤id

        String y_createdby = tabcValue.getString("c_user_id"); //工单创建人
        String y_createdgroup = tabcValue.getString("c_group_id"); //创建组
        String sub_doctype = tabcValue.getString("sub_doctype"); //发送来源(页面ID)

        String customer_guid = tabcValue.getJSONObject("data").getString("customer_guid"); //当前customer_guid
        String workorderid = tabcValue.getJSONObject("data").getString("workorderid"); //工单流水ID
        String businessnumber = tabcValue.getJSONObject("data").getString("businessnumber"); //物业号
        String workordertype1 = tabcValue.getJSONObject("data").getString("workordertype1"); //受理渠道
        //String ys_code_id = inJson.getJSONObject("data").getString("ys_code_id"); //工单标识
        String y_customerproblem = in_workorderflowdetail; //客户问题
        String tabcname = tabcValue.getString("tabcname");


        JSONObject tabcJSON = tabcValue.getJSONObject("data").getJSONObject("business_info");//获取tabc字段信息
        log.debug("tabcJSON:" + tabcJSON.toString())
        fieldValue.putAll(tabcJSON);
        String fieldName = "";
        String valueString = "";
        String updateString = ""
        List<Object> valueList = new ArrayList<Object>();
        for (String key : fieldValue.keySet()) {
            if (fieldName == "") {
                fieldName = key;
                valueString = "?";
                updateString = key + "=" + "?";
                insertString = key;
            } else {
                fieldName = fieldName + "," + key;
                valueString = valueString + ",?";
                updateString = updateString + "," + key + "=" + "?";
                insertString = insertString + "," + key;
            }
            //valueList.add(fieldValue.get(key));
            if (!String.valueOf(fieldValue.get(key)).equals("null")) {
                valueList.add(KHUtil.isNull(String.valueOf(fieldValue.get(key))) ? null : fieldValue.get(key));
            } else {
                valueList.add(fieldValue.get(key));
            }
        }
        if (fieldName == "") {
            result.put("code", "-1");
            result.put("msg", "字段不能为空")
            return result;
        }

        String Relation_ID = UUID.randomUUID().toString();
        String[] data1 = new String[3];

        String nodeflag = "select MULTITASK,TASKFLAG from wo3_node_map where NODEMAP_ID=:step_id and map_id=:map_id";
        MapSqlParameterSource nodeflagList = new MapSqlParameterSource();
        nodeflagList.addValue("step_id", step_id);
        nodeflagList.addValue("map_id", map_id);

        List<Map<String, Object>> mapnode = eliteBean.chaxun_sql_pro(nodeflag, nodeflagList, "dataSource");
        if (mapnode.size() == 0) {
            result.put("code", "-1");
            result.put("msg", "当前步骤id不对")
            return result;
        }

        String MULTITASK = "";
        String TASKFLAG = "";
        for (Map<String, Object> numsorder : mapnode) {
            MULTITASK = numsorder.get("MULTITASK").toString();
            TASKFLAG = numsorder.get("TASKFLAG").toString();
        }

        if (TASKFLAG == "1") {
            String nodes = "select * from wo3_link_map where source_id=:step_id  and map_id=:map_id";
            List<Map<String, Object>> nodelists = eliteBean.chaxun_sql_pro(nodes, nodeflagList, "dataSource");
            String[] elitetasksql = new String[nodelists.size()];

        } else {
            String nodes = "select wlm.target_id,wnm.handlegroup_id,wnm.handleby_id,wnm.sendtype from wo3_link_map wlm left join wo3_node_map wnm on wlm.target_id=wnm.NODEMAP_ID and wlm.map_id=wnm.map_id where wlm.source_id=:step_id and wlm.map_id=:map_id";
            List<Map<String, Object>> nodelists = eliteBean.chaxun_sql_pro(nodes, nodeflagList, "dataSource");
            Map<String, Object> nextNodeMap = nodelists.get(0);
            String nextNode = nextNodeMap.get("target_id");//下一步节点id
            String handlerole_id = "";
            String handlegroup_id = "";
            if (nextNodeMap.get("sendtype").toString() == "10") {//类型：组加角色
                handlerole_id = nextNodeMap.get("handleby_id");//角色
                handlegroup_id = nextNodeMap.get("handlegroup_id");//组
            } else if (nextNodeMap.get("sendtype").toString() == "3") {//类型：角色
                handlerole_id = nextNodeMap.get("handlegroup_id");//角色
            } else if (nextNodeMap.get("sendtype").toString() == "2") {//类型：指定人
                handlegroup_id = nextNodeMap.get("handlegroup_id");//组
                in_r_staff_id = nextNodeMap.get("handleby_id");//人
            } else if (nextNodeMap.get("sendtype").toString() == "1") {//类型：指定组
                handlegroup_id = nextNodeMap.get("handlegroup_id");//组
                in_r_staff_id = nextNodeMap.get("handleby_id");//人
            }

            if (in_r_group_id) {
                handlegroup_id = in_r_group_id;//组
                in_r_staff_id = tabcValue.getString("in_r_staff_id");//人
            }

            valueList.add(Relation_ID);
            valueString = valueString + ',?';
            String tabcsql = "insert into " + tabcname + "(" + fieldName + ",relation_id) value(" + valueString + ")";
            int tabcResule = jt.update(tabcsql, valueList.toArray());
            if (tabcResule > 0) {
                if (in_r_staff_id == "") {
                    data1[0] = "Insert Into Objective (Objective_Guid, Objectivetype_Id, Handleby_Id, Handlegroup_Id, Priority_Id, Expireddate, Comments, Customer_Guid, Firstcontactdate, Lastcontactdate, Objectivestatus, Iscontact, Trytimes, Successfulcontacttimes, Createdby, Createddate, Modifiedby, Modifieddate, Expecttradedate, Failreason, Quantity, Amount, Obstarttime, Obendtime, Firstcalldate, Failure_Attempt, Passflag, Localtrycount, Lastassigndate, Isanswer, Prepossibility, Expectstarttime, Workorderframe_Id, Category, Forecastdate, Workorderid, Objectivesubstatus, Map_Id, Curstep_Id_Map, Max_Attempt, Nocontacttry, handlerole_id, businessnumber, workordertype1,DATATYPE) Values ('" + Relation_ID + "', '" + frame_id + "', null, '" + handlegroup_id + "', 'NORMAL', now(), '" + in_workorderflowdetail + "','" + customer_guid + "', Null, Null, 'OPEN', '0', '0', '0', '" + y_createdby + "', now(), '" + y_createdby + "', now(), Null, '', '0', '0', Null, Null, Null, '0', '0', '0', Null, '0', '.00', Null, '" + map_id + "', 'CASE1', now(), '" + workorderid + "', 'OPEN ', '" + map_id + "', '" + nextNode + "', '0', '0','" + handlerole_id + "','" + businessnumber + "','" + workordertype1 + "','CUST')";
                    data1[1] = "Insert Into Elitetask (Elitetask_Guid, Objective_Guid, Objectivetype_Id, Expectstarttime, Expectendtime, Priority, Handleby_Id, Handlegroup_Id, Createdby, Createddate, Modifiedby, Modifieddate, Comments, Customer_Guid, Customerlevel, Localtrycount, Curstepstatus, Curstep_Id, Stepforecastdate, Curstep_Id_Map, Map_Id, Objectivesubstatus, Elitetaskstatus, Obflag,handlerole_id) Values (uuid(), '" + Relation_ID + "', '" + frame_id + "', now(), now(), 'NORMAL',null, '" + handlegroup_id + "', '" + y_createdby + "', now(), '" + y_createdby + "', now(), '" + in_workorderflowdetail + "', '" + customer_guid + "', Null, '0', 'OPEN ', '" + nextNode + "', now(), '" + nextNode + "', '" + map_id + "', 'OPEN', Null, '0','" + handlerole_id + "')";
                    data1[2] = "Insert Into Workorderstep3 (Workorder_Guid, Workorderstep_Guid, oldHandleby_Id, oldHandlegroup_Id, Workorderstepstatus, Comments, Modifieddate, From_Stepidmap, to_stepidmap, Workstarttime, Workendtime, Handlegroup_Id, handlerole_id) Values ('" + Relation_ID + "', uuid(), '" + y_createdby + "', '" + y_createdgroup + "', 'SENDNS', '" + in_workorderflowdetail + "', now(), '" + step_id + "','" + nextNode + "', now(), now(),'" + handlegroup_id + "','" + handlerole_id + "')";
                } else {
                    data1[0] = "Insert Into Objective (Objective_Guid, Objectivetype_Id, Handleby_Id, Handlegroup_Id, Priority_Id, Expireddate, Comments, Customer_Guid, Firstcontactdate, Lastcontactdate, Objectivestatus, Iscontact, Trytimes, Successfulcontacttimes, Createdby, Createddate, Modifiedby, Modifieddate, Expecttradedate, Failreason, Quantity, Amount, Obstarttime, Obendtime, Firstcalldate, Failure_Attempt, Passflag, Localtrycount, Lastassigndate, Isanswer, Prepossibility, Expectstarttime, Workorderframe_Id, Category, Forecastdate, Workorderid, Objectivesubstatus, Map_Id, Curstep_Id_Map, Max_Attempt, Nocontacttry, businessnumber, workordertype1,DATATYPE) Values ('" + Relation_ID + "', '" + frame_id + "', '" + in_r_staff_id + "', '" + handlegroup_id + "', 'NORMAL', now(), '" + in_workorderflowdetail + "','" + customer_guid + "', Null, Null, 'OPEN', '0', '0', '0', '" + y_createdby + "', now(), '" + y_createdby + "', now(), Null, '', '0', '0', Null, Null, Null, '0', '0', '0', Null, '0', '.00', Null, '" + map_id + "', 'CASE1', now(), '" + workorderid + "', 'OPEN ', '" + map_id + "', '" + nextNode + "', '0', '0','" + businessnumber + "','" + workordertype1 + "','CUST')";
                    data1[1] = "Insert Into Elitetask (Elitetask_Guid, Objective_Guid, Objectivetype_Id, Expectstarttime, Expectendtime, Priority, Handleby_Id, Handlegroup_Id, Createdby, Createddate, Modifiedby, Modifieddate, Comments, Customer_Guid, Customerlevel, Localtrycount, Curstepstatus, Curstep_Id, Stepforecastdate, Curstep_Id_Map, Map_Id, Objectivesubstatus, Elitetaskstatus, Obflag) Values (uuid(), '" + Relation_ID + "', '" + frame_id + "', now(), now(), 'NORMAL', '" + in_r_staff_id + "', '" + handlegroup_id + "', '" + y_createdby + "', now(), '" + y_createdby + "', now(), '" + in_workorderflowdetail + "', '" + customer_guid + "', Null, '0', 'OPEN ', '" + nextNode + "', now(), '" + nextNode + "', '" + map_id + "', 'OPEN', Null, '0')";
                    data1[2] = "Insert Into Workorderstep3 (Workorder_Guid, Workorderstep_Guid, oldHandleby_Id, oldHandlegroup_Id, Workorderstepstatus, Comments, Modifieddate, From_Stepidmap, to_stepidmap, Workstarttime, Workendtime, handleby_id, handlegroup_id, handlerole_id) Values ('" + Relation_ID + "', uuid(), '" + y_createdby + "', '" + y_createdgroup + "', 'SENDNS', '" + in_workorderflowdetail + "', now(), '" + step_id + "','" + nextNode + "', now(), now(),'" + in_r_staff_id + "','" + handlegroup_id + "',null)";
                }
                log.debug("判断处理工单状态进行下一步处理SQL:" + data1.toString());
                int[] dataresult1 = eliteBean.updateBatch(data1, "dataSource");

                sendSMStoStaff(eliteBean, handlegroup_id, in_r_staff_id, customer_guid, workorderid, map_id, Relation_ID, "SEND", y_createdby, y_createdgroup, sub_doctype);
                sendSMStoAPP(eliteBean, handlegroup_id, in_r_staff_id, customer_guid, workorderid, map_id, Relation_ID, "APPSEND", y_createdby, y_createdgroup, sub_doctype);
                sendSMStoCustomer(eliteBean, customer_guid, workorderid, map_id, Relation_ID, "Create", y_createdby, y_createdgroup, sub_doctype);
            }
        }
        result.put("code", "1");
        //result.put("msg", "创建成功")
        return result;
    }

    //下送工单
    public JSONObject SendOrder(JdbcTemplate jt, JSONObject tabcValue, EliteBean eliteBean) {
        log.debug("下送工单操作入参完整信息:" + tabcValue.toString());
        Map fieldValue = new HashMap<String, Object>();
        JSONObject result = new JSONObject();

        String in_s_objetive_guid = tabcValue.getJSONObject("data").getString("in_s_objetive_guid"); //当前objective_guid
        String in_s_elitetask_guid = tabcValue.getJSONObject("data").getString("in_s_elitetask_guid"); //当前objective_guid
        String in_r_staff_id = tabcValue.getString("in_r_staff_id"); //下送人
        String in_r_group_id = tabcValue.getString("in_r_group_id"); //下送组
        String in_workorderflowdetail = tabcValue.getJSONObject("data").getString("in_workorderflowdetail"); // 工单处理内容描述
        String map_id = tabcValue.getString("map_id"); //工单类型id
        String frame_id = tabcValue.getString("frame_id"); //工单模板id
        String step_id = tabcValue.getString("step_id"); //工单步骤id
        String customer_guid = tabcValue.getJSONObject("data").getString("customer_guid"); //当前customer_guid
        String workorderid = tabcValue.getJSONObject("data").getString("workorderid"); //工单流水ID
        String y_createdby = tabcValue.getString("c_user_id"); //工单创建人
        String y_createdgroup = tabcValue.getString("c_group_id"); //创建组
        String sub_doctype = tabcValue.getString("sub_doctype"); //发送来源(页面ID)
        String y_customerproblem = in_workorderflowdetail; //客户问题
        String tabcname = tabcValue.getString("tabcname");
        log.debug("界面工单字段信息 in_s_objetive_guid:" + in_s_objetive_guid + " in_s_elitetask_guid:" + in_s_elitetask_guid + " in_r_staff_id:" + in_r_staff_id + " in_r_group_id:" + in_r_group_id + " in_workorderflowdetail:" + in_workorderflowdetail + " map_id:" + map_id + " frame_id:" + frame_id + " step_id:" + step_id + " customer_guid:" + customer_guid + " workorderid:" + workorderid + " y_createdby:" + y_createdby + " y_createdgroup:" + y_createdgroup + " sub_doctype:" + sub_doctype + " y_customerproblem:" + y_customerproblem + " tabcname:" + tabcname);

        JSONObject tabcJSON = tabcValue.getJSONObject("data").getJSONObject("business_info");//获取tabc字段信息
        fieldValue.putAll(tabcJSON);
        String fieldName = "";
        String valueString = "";
        String updateString = ""
        List<Object> valueList = new ArrayList<Object>();
        for (String key : fieldValue.keySet()) {
            if (fieldName == "") {
                fieldName = key;
                valueString = "?";
                updateString = key + "=" + "?";
                insertString = key;
            } else {
                fieldName = fieldName + "," + key;
                valueString = valueString + ",?";
                updateString = updateString + "," + key + "=" + "?";
                insertString = insertString + "," + key;
            }
            //valueList.add(fieldValue.get(key));
            if (!String.valueOf(fieldValue.get(key)).equals("null")) {
                valueList.add(KHUtil.isNull(String.valueOf(fieldValue.get(key))) ? null : fieldValue.get(key));
            } else {
                valueList.add(fieldValue.get(key));
            }
        }
        log.debug("界面Keys信息fieldList:" + fieldName);
        log.debug("界面Values信息valueList:" + valueList.toString());

        if (fieldName == "") {
            result.put("code", "-1");
            result.put("msg", "字段不能为空")
            return result;
        }

        List<Map<String, Object>> ordernums = jt.queryForList("select count(1) nums from objective where objective_guid='" + in_s_objetive_guid + "'");
        String ordernum = "0";
        for (Map<String, Object> numsorder : ordernums) {
            ordernum = numsorder.get("nums").toString();
        }
        log.debug("【ordernum】：" + fieldName);
        if (ordernum == "0") {

            customer_guid = tabcValue.getJSONObject("data").getString("customer_guid"); //当前customer_guid
            workorderid = tabcValue.getJSONObject("data").getString("workorderid"); //工单流水ID
            String Relation_ID = UUID.randomUUID().toString();
            String[] data1 = new String[3];

            String nodeflag = "select MULTITASK, TASKFLAG from wo3_node_map where NODEMAP_ID=:step_id and map_id=:map_id";
            MapSqlParameterSource nodeflagList = new MapSqlParameterSource();
            nodeflagList.addValue("step_id", step_id);
            nodeflagList.addValue("map_id", map_id);

            List<Map<String, Object>> mapnode = eliteBean.chaxun_sql_pro(nodeflag, nodeflagList, "dataSource");
            if (mapnode.size() == 0) {
                result.put("code", "-1");
                result.put("msg", "当前步骤id不对")
                return result;
            }
            String MULTITASK = "";
            String TASKFLAG = "";
            for (Map<String, Object> numsorder : mapnode) {
                MULTITASK = numsorder.get("MULTITASK").toString();
                TASKFLAG = numsorder.get("TASKFLAG").toString();
            }

            if (TASKFLAG == "1") {
                String nodes = "select * from wo3_link_map where source_id=:step_id and map_id=:map_id";
                List<Map<String, Object>> nodelists = eliteBean.chaxun_sql_pro(nodes, nodeflagList, "dataSource");
                String[] elitetasksql = new String[nodelists.size()];

            } else {
                String nodes = "select wlm.target_id, wnm.handlegroup_id, wnm.handleby_id, wnm.sendtype from wo3_link_map wlm left join wo3_node_map wnm on wlm.target_id=wnm.NODEMAP_ID and wlm.map_id=wnm.map_id where wlm.source_id=:step_id and wlm.map_id=:map_id";
                List nodelists = eliteBean.chaxun_sql_pro(nodes, nodeflagList, "dataSource");
                Map nextNodeMap = nodelists.get(0);
                String nextNode = nextNodeMap.get("target_id");//下一步节点id
                String handlerole_id = "";
                String handlegroup_id = "";
                if (nextNodeMap.get("sendtype").toString() == "10") {//类型：组加角色
                    handlerole_id = nextNodeMap.get("handleby_id");//角色
                    handlegroup_id = nextNodeMap.get("handlegroup_id");//组
                } else if (nextNodeMap.get("sendtype").toString() == "3") {//类型：角色
                    handlerole_id = nextNodeMap.get("handlegroup_id");//角色
                } else if (nextNodeMap.get("sendtype").toString() == "2") {//类型：指定人
                    handlegroup_id = nextNodeMap.get("handlegroup_id");//组
                    in_r_staff_id = nextNodeMap.get("handleby_id");//人
                } else if (nextNodeMap.get("sendtype").toString() == "1") {//类型：指定组
                    handlegroup_id = nextNodeMap.get("handlegroup_id");//组
                    in_r_staff_id = nextNodeMap.get("handleby_id");//人
                }
                if (in_r_group_id) {
                    handlegroup_id = in_r_group_id;//组
                    in_r_staff_id = tabcValue.getString("in_r_staff_id");//人
                }
                log.debug("角色：" + handlerole_id);
                log.debug("组：" + handlegroup_id);
                valueList.add(Relation_ID);
                valueString = valueString + ',?';
                String tabcsql = "insert into " + tabcname + "(" + fieldName + ",relation_id) value(" + valueString + ")";
                int tabcResule = jt.update(tabcsql, valueList.toArray());
                if (tabcResule > 0) {
                    log.debug("in_r_staff_id:" + in_r_staff_id);
                    if (!in_r_staff_id) {
                        data1[0] = "Insert Into Objective (Objective_Guid, Objectivetype_Id, Handleby_Id, Handlegroup_Id, Priority_Id, Expireddate, Comments,Customer_Guid, Firstcontactdate, Lastcontactdate, Objectivestatus, Iscontact, Trytimes, Successfulcontacttimes, Createdby, Createddate, Modifiedby, Modifieddate, Expecttradedate, Failreason, Quantity, Amount, Obstarttime, Obendtime, Firstcalldate, Failure_Attempt, Passflag, Localtrycount, Lastassigndate, Isanswer, Prepossibility, Expectstarttime, Workorderframe_Id, Category, Forecastdate, Workorderid, Objectivesubstatus, Map_Id, Curstep_Id_Map, Max_Attempt, Nocontacttry, handlerole_id,DATATYPE) Values ('" + Relation_ID + "', '" + frame_id + "', null, '" + handlegroup_id + "', 'NORMAL', now(), '" + in_workorderflowdetail + "','" + customer_guid + "', Null, Null, 'OPEN', '0', '0', '0', '" + y_createdby + "', now(), '" + y_createdby + "', now(), Null, '', '0', '0', Null, Null, Null, '0', '0', '0', Null, '0', '.00', Null, '" + map_id + "', 'CASE1', now(), '" + workorderid + "', 'OPEN ', '" + map_id + "', '" + nextNode + "', '0', '0','" + handlerole_id + "','CUST')";
                        data1[1] = "Insert Into Elitetask (Elitetask_Guid, Objective_Guid, Objectivetype_Id, Expectstarttime, Expectendtime, Priority, Handleby_Id, Handlegroup_Id, Createdby, Createddate, Modifiedby, Modifieddate, Comments, Customer_Guid, Customerlevel, Localtrycount, Curstepstatus, Curstep_Id, Stepforecastdate, Curstep_Id_Map, Map_Id, Objectivesubstatus, Elitetaskstatus, Obflag, handlerole_id) Values (uuid(), '" + Relation_ID + "', '" + frame_id + "', now(), now(), 'NORMAL',null, '" + handlegroup_id + "', '" + y_createdby + "', now(), '" + y_createdby + "', now(), '" + in_workorderflowdetail + "', '" + customer_guid + "', Null, '0', 'OPEN ', '" + nextNode + "', now(), '" + nextNode + "', '" + map_id + "', 'OPEN', Null, '0','" + handlerole_id + "')";
                        data1[2] = "Insert Into Workorderstep3 (Workorder_Guid, Workorderstep_Guid, oldHandleby_Id, oldHandlegroup_Id, Workorderstepstatus, Comments, Modifieddate, From_Stepidmap,to_stepidmap, Workstarttime, Workendtime, Handlegroup_Id, handlerole_id) Values ('" + in_s_objetive_guid + "', uuid(), '" + y_createdby + "', '" + y_createdgroup + "', 'SENDNS', '" + in_workorderflowdetail + "', now(), '" + step_id + "','" + nextNode + "', now(), now(),'" + handlegroup_id + "','" + handlerole_id + "')";
                    } else {
                        data1[0] = "Insert Into Objective (Objective_Guid, Objectivetype_Id, Handleby_Id, Handlegroup_Id, Priority_Id, Expireddate, Comments,Customer_Guid, Firstcontactdate, Lastcontactdate, Objectivestatus, Iscontact, Trytimes, Successfulcontacttimes, Createdby, Createddate, Modifiedby, Modifieddate, Expecttradedate, Failreason, Quantity, Amount, Obstarttime, Obendtime, Firstcalldate, Failure_Attempt, Passflag, Localtrycount, Lastassigndate, Isanswer, Prepossibility, Expectstarttime, Workorderframe_Id, Category, Forecastdate, Workorderid, Objectivesubstatus, Map_Id, Curstep_Id_Map, Max_Attempt, Nocontacttry,DATATYPE) Values ('" + Relation_ID + "', '" + frame_id + "', '" + in_r_staff_id + "', '" + handlegroup_id + "', 'NORMAL', now(), '" + in_workorderflowdetail + "','" + customer_guid + "', Null, Null, 'OPEN', '0', '0', '0', '" + y_createdby + "', now(), '" + y_createdby + "', now(), Null, '', '0', '0', Null, Null, Null, '0', '0', '0', Null, '0', '.00', Null, '" + map_id + "', 'CASE1', now(), '" + workorderid + "', 'OPEN ', '" + map_id + "', '" + nextNode + "', '0', '0','CUST')";
                        data1[1] = "Insert Into Elitetask (Elitetask_Guid, Objective_Guid, Objectivetype_Id, Expectstarttime, Expectendtime, Priority, Handleby_Id, Handlegroup_Id, Createdby, Createddate, Modifiedby, Modifieddate, Comments, Customer_Guid, Customerlevel, Localtrycount, Curstepstatus, Curstep_Id, Stepforecastdate, Curstep_Id_Map, Map_Id, Objectivesubstatus, Elitetaskstatus, Obflag) Values (uuid(), '" + Relation_ID + "', '" + frame_id + "', now(), now(), 'NORMAL', '" + in_r_staff_id + "', '" + handlegroup_id + "', '" + y_createdby + "', now(), '" + y_createdby + "', now(), '" + in_workorderflowdetail + "', '" + customer_guid + "', Null, '0', 'OPEN ', '" + nextNode + "', now(), '" + nextNode + "', '" + map_id + "', 'OPEN', Null, '0')";
                        data1[2] = "Insert Into Workorderstep3 (Workorder_Guid, Workorderstep_Guid, oldHandleby_Id, oldHandlegroup_Id, Workorderstepstatus, Comments, Modifieddate, From_Stepidmap,to_stepidmap, Workstarttime, Workendtime, handleby_id, handlegroup_id, handlerole_id) Values ('" + in_s_objetive_guid + "', uuid(), '" + y_createdby + "', '" + y_createdgroup + "', 'SENDNS', '" + in_workorderflowdetail + "', now(), '" + step_id + "','" + nextNode + "', now(), now(),'" + in_r_staff_id + "','" + handlegroup_id + "',null)";
                    }
                    log.debug("判断处理工单状态进行下一步处理SQL:" + data1.toString());
                    int[] dataresult1 = eliteBean.updateBatch(data1, "dataSource");
                }
            }
        } else if (ordernum > "0") {
            String nodeflag = "select MULTITASK,TASKFLAG from wo3_node_map where NODEMAP_ID=:step_id and map_id=:map_id";
            MapSqlParameterSource nodeflagList = new MapSqlParameterSource();
            nodeflagList.addValue("step_id", step_id);
            nodeflagList.addValue("map_id", map_id);
            log.debug("步骤：" + nodeflagList);

            List<Map<String, Object>> mapnode = eliteBean.chaxun_sql_pro(nodeflag, nodeflagList, "dataSource");
            if (mapnode.size() == 0) {
                result.put("code", "-1");
                result.put("msg", "当前步骤id不对")
                return result;
            }
            String MULTITASK = "";
            String TASKFLAG = "";
            for (Map<String, Object> numsorder : mapnode) {
                MULTITASK = numsorder.get("MULTITASK").toString();
                TASKFLAG = numsorder.get("TASKFLAG").toString();
            }
            log.debug("MULTITASK:" + MULTITASK);
            log.debug("TASKFLAG:" + TASKFLAG);
            if (TASKFLAG == "1") {
                String nodes = "select * from wo3_link_map where source_id=:step_id and map_id=:map_id";
                List<Map<String, Object>> nodelists = eliteBean.chaxun_sql_pro(nodes, nodeflagList, "dataSource");
                String[] elitetasksql = new String[nodelists.size()];
            } else {
                //查 elitetask 判断是否是多流程工单
                List<Map<String, Object>> tasknums = jt.queryForList("select count(1) nums from elitetask where objective_guid='" + in_s_objetive_guid + "'");
                String tasknum = "0";
                for (Map<String, Object> numsorder : tasknums) {
                    tasknum = numsorder.get("nums").toString();
                }
                log.debug("【tasknum】：" + tasknum);
                if(tasknum == "1"){
                    String[] data1 = new String[3];
                    String nodes = "select wlm.target_id,wnm.handlegroup_id,wnm.handleby_id,wnm.sendtype from wo3_link_map wlm left join wo3_node_map wnm on wlm.target_id=wnm.NODEMAP_ID and wlm.map_id=wnm.map_id where wlm.source_id=:step_id and wlm.map_id=:map_id";
                    List<Map<String, Object>> nodelists = eliteBean.chaxun_sql_pro(nodes, nodeflagList, "dataSource");
                    Map<String, Object> nextNodeMap = nodelists.get(0);
                    String nextNode = nextNodeMap.get("target_id");//下一步节点id
                    String handlerole_id, handlegroup_id;
                    if (nextNodeMap.get("sendtype").toString() == "10") {//类型：组加角色
                        handlerole_id = nextNodeMap.get("handleby_id");//角色
                        handlegroup_id = nextNodeMap.get("handlegroup_id");//组
                    } else if (nextNodeMap.get("sendtype").toString() == "3") {//类型：角色
                        handlerole_id = nextNodeMap.get("handlegroup_id");//角色
                    } else if (nextNodeMap.get("sendtype").toString() == "2") {//类型：指定人
                        handlegroup_id = nextNodeMap.get("handlegroup_id");//组
                        in_r_staff_id = nextNodeMap.get("handleby_id");//人
                    } else if (nextNodeMap.get("sendtype").toString() == "1") {//类型：指定组
                        handlegroup_id = nextNodeMap.get("handlegroup_id");//组
                        in_r_staff_id = nextNodeMap.get("handleby_id");//人
                    }else if (nextNodeMap.get("sendtype").toString() == "4") {//类型：原步骤
                        handlegroup_id = nextNodeMap.get("handlegroup_id");//原步骤的节点
                        in_r_staff_id = nextNodeMap.get("handleby_id");//人
                        String nodes1 = "select OLDHANDLEBY_ID, OLDHANDLEGROUP_ID from workorderstep3 where FROM_STEPIDMAP='"+handlegroup_id+"' and WORKORDER_GUID='"+in_s_objetive_guid+"' order by MODIFIEDDATE asc";
                        List<Map<String, Object>> nodelists1 = eliteBean.chaxun_sql(nodes1, "dataSource");
                        handlegroup_id = nodelists1.get(0).get("OLDHANDLEGROUP_ID");//组
                        in_r_staff_id = nodelists1.get(0).get("OLDHANDLEBY_ID");//人
                    }
                    if (in_r_group_id) {
                        handlegroup_id = in_r_group_id;//组
                        in_r_staff_id = tabcValue.getString("in_r_staff_id");//人
                    }
                    eliteBean.update("insert into " + tabcname + "_his (hisguid," + insertString + ") select uuid()," + insertString + " from " + tabcname + " t where RELATION_ID='" + in_s_objetive_guid + "'", "dataSource");
                    valueList.add(in_s_objetive_guid);
                    String tabcsql = "update " + tabcname + " set " + updateString + " where relation_id=?";
                    int tabcResule = jt.update(tabcsql, valueList.toArray());
                    if (tabcResule > 0) {
                        //data1[0] = "insert into tabcwo_business_info_his select uuid(),t.* from tabcwo_business_info t where RELATION_ID='" + in_s_objetive_guid + "'";
                        if (in_r_staff_id == "") {
                            data1[0] = "Update Objective Set handleby_id=null, Handlegroup_Id = '" + handlegroup_id + "', Curstep_Id_Map = '" + nextNode + "', comments='" + in_workorderflowdetail + "', Modifiedby = '" + y_createdby + "', Modifieddate = now(), handlerole_id='" + handlerole_id + "' Where Objective_Guid = '" + in_s_objetive_guid + "'";
                            data1[1] = "Update Elitetask Set handleby_id=null, Handlegroup_Id = '" + handlegroup_id + "', Handleby_Id = '" + in_r_staff_id + "', Curstep_Id_Map = '" + nextNode + "', comments = '" + in_workorderflowdetail + "', Modifiedby = '" + y_createdby + "', Modifieddate = now(), handlerole_id='" + handlerole_id + "' Where elitetask_guid = '" + in_s_elitetask_guid + "'";
                            data1[2] = "Insert Into Workorderstep3 (Workorder_Guid, Workorderstep_Guid, oldHandleby_Id, oldHandlegroup_Id, Workorderstepstatus, Comments, Modifieddate, From_Stepidmap, to_stepidmap, Workstarttime, Workendtime, Handlegroup_Id, handlerole_id) Values ('" + in_s_objetive_guid + "', uuid(), '" + y_createdby + "', '" + y_createdgroup + "', 'SENDNS', '" + in_workorderflowdetail + "', now(), '" + step_id + "', '" + nextNode + "', now(), now(),'" + handlegroup_id + "','" + handlerole_id + "')";
                        } else {
                            data1[0] = "Update Objective Set handleby_id='" + in_r_staff_id + "', Handlegroup_Id = '" + handlegroup_id + "', Curstep_Id_Map = '" + nextNode + "', comments='" + in_workorderflowdetail + "', Modifiedby = '" + y_createdby + "', Modifieddate = now(), handlerole_id='" + handlerole_id + "' Where Objective_Guid = '" + in_s_objetive_guid + "'";
                            data1[1] = "Update Elitetask Set handleby_id='" + in_r_staff_id + "', Handlegroup_Id = '" + handlegroup_id + "', Handleby_Id = '" + in_r_staff_id + "', Curstep_Id_Map = '" + nextNode + "', comments = '" + in_workorderflowdetail + "', Modifiedby = '" + y_createdby + "', Modifieddate = now() Where elitetask_guid = '" + in_s_elitetask_guid + "'";
                            data1[2] = "Insert Into Workorderstep3 (Workorder_Guid, Workorderstep_Guid, oldHandleby_Id, oldHandlegroup_Id, Workorderstepstatus, Comments, Modifieddate, From_Stepidmap, to_stepidmap, Workstarttime, Workendtime, handleby_id, handlegroup_id, handlerole_id) Values ('" + in_s_objetive_guid + "', uuid(), '" + y_createdby + "', '" + y_createdgroup + "', 'SENDNS', '" + in_workorderflowdetail + "', now(), '" + step_id + "','" + nextNode + "', now(), now(),'" + in_r_staff_id + "','" + handlegroup_id + "',null)";
                        }
                        log.debug("判断处理工单状态进行下一步处理SQL:" + data1.toString());
                        int[] dataresult1 = eliteBean.updateBatch(data1, "dataSource");

                        sendSMStoStaff(eliteBean, handlegroup_id, in_r_staff_id, customer_guid, workorderid, map_id, in_s_objetive_guid, "SEND", y_createdby, y_createdgroup, sub_doctype);
                        sendSMStoAPP(eliteBean, handlegroup_id, in_r_staff_id, customer_guid, workorderid, map_id, in_s_objetive_guid, "APPSEND", y_createdby, y_createdgroup, sub_doctype);
                    }
                } else if(tasknum > "1"){
                    String[] data1 = new String[3];
                    String nodes = "select wlm.target_id,wnm.handlegroup_id,wnm.handleby_id,wnm.sendtype from wo3_link_map wlm left join wo3_node_map wnm on wlm.target_id=wnm.NODEMAP_ID and wlm.map_id=wnm.map_id where wlm.source_id=:step_id and wlm.map_id=:map_id";
                    List<Map<String, Object>> nodelists = eliteBean.chaxun_sql_pro(nodes, nodeflagList, "dataSource");
                    Map<String, Object> nextNodeMap = nodelists.get(0);
                    String nextNode = nextNodeMap.get("target_id");//下一步节点id
                    String handlerole_id, handlegroup_id;
                    if (nextNodeMap.get("sendtype").toString() == "10") {//类型：组加角色
                        handlerole_id = nextNodeMap.get("handleby_id");//角色
                        handlegroup_id = nextNodeMap.get("handlegroup_id");//组
                    } else if (nextNodeMap.get("sendtype").toString() == "3") {//类型：角色
                        handlerole_id = nextNodeMap.get("handlegroup_id");//角色
                    } else if (nextNodeMap.get("sendtype").toString() == "2") {//类型：指定人
                        handlegroup_id = nextNodeMap.get("handlegroup_id");//组
                        in_r_staff_id = nextNodeMap.get("handleby_id");//人
                    } else if (nextNodeMap.get("sendtype").toString() == "1") {//类型：指定组
                        handlegroup_id = nextNodeMap.get("handlegroup_id");//组
                        in_r_staff_id = nextNodeMap.get("handleby_id");//人
                    }else if (nextNodeMap.get("sendtype").toString() == "4") {//类型：原步骤
                        handlegroup_id = nextNodeMap.get("handlegroup_id");////原步骤的节点
                        in_r_staff_id = nextNodeMap.get("handleby_id");//人
                        String nodes1 = "select OLDHANDLEBY_ID, OLDHANDLEGROUP_ID from workorderstep3 where FROM_STEPIDMAP='"+handlegroup_id+"' and WORKORDER_GUID='"+in_s_objetive_guid+"' order by MODIFIEDDATE asc";
                        List<Map<String, Object>> nodelists1 = eliteBean.chaxun_sql(nodes1, "dataSource");
                        handlegroup_id = nodelists1.get(0).get("OLDHANDLEGROUP_ID");//组
                        in_r_staff_id = nodelists1.get(0).get("OLDHANDLEBY_ID");//人
                    }
                    if (in_r_group_id) {
                        handlegroup_id = in_r_group_id;//组
                        in_r_staff_id = tabcValue.getString("in_r_staff_id");//人
                    }
                    eliteBean.update("insert into " + tabcname + "_his (hisguid," + insertString + ") select uuid()," + insertString + " from " + tabcname + " t where RELATION_ID='" + in_s_objetive_guid + "'", "dataSource");
                    valueList.add(in_s_objetive_guid);
                    String tabcsql = "update " + tabcname + " set " + updateString + " where relation_id=?";
                    int tabcResule = jt.update(tabcsql, valueList.toArray());
                    if (tabcResule > 0) {
                        if (in_r_staff_id == "") {
                            data1[0] = "Update Objective Set handleby_id=null, Handlegroup_Id = '" + handlegroup_id + "', Curstep_Id_Map = '" + nextNode + "', comments='" + in_workorderflowdetail + "', Modifiedby = '" + y_createdby + "', Modifieddate = now(), handlerole_id='" + handlerole_id + "' Where Objective_Guid = '" + in_s_objetive_guid + "'";
                            data1[1] = "Delete from Elitetask Where elitetask_guid = '" + in_s_elitetask_guid + "'";
                            data1[2] = "Insert Into Workorderstep3 (Workorder_Guid, Workorderstep_Guid, oldHandleby_Id, oldHandlegroup_Id, Workorderstepstatus, Comments, Modifieddate, From_Stepidmap, to_stepidmap, Workstarttime, Workendtime, Handlegroup_Id, handlerole_id) Values ('" + in_s_objetive_guid + "', uuid(), '" + y_createdby + "', '" + y_createdgroup + "', 'SENDNS', '" + in_workorderflowdetail + "', now(), '" + step_id + "', '" + nextNode + "', now(), now(),'" + handlegroup_id + "','" + handlerole_id + "')";
                        } else {
                            data1[0] = "Update Objective Set handleby_id='" + in_r_staff_id + "', Handlegroup_Id = '" + handlegroup_id + "', Curstep_Id_Map = '" + nextNode + "', comments='" + in_workorderflowdetail + "', Modifiedby = '" + y_createdby + "', Modifieddate = now(), handlerole_id='" + handlerole_id + "' Where Objective_Guid = '" + in_s_objetive_guid + "'";
                            data1[1] = "Delete from Elitetask Where elitetask_guid = '" + in_s_elitetask_guid + "'";
                            data1[2] = "Insert Into Workorderstep3 (Workorder_Guid, Workorderstep_Guid, oldHandleby_Id, oldHandlegroup_Id, Workorderstepstatus, Comments, Modifieddate, From_Stepidmap, to_stepidmap, Workstarttime, Workendtime, handleby_id, handlegroup_id, handlerole_id) Values ('" + in_s_objetive_guid + "', uuid(), '" + y_createdby + "', '" + y_createdgroup + "', 'SENDNS', '" + in_workorderflowdetail + "', now(), '" + step_id + "','" + nextNode + "', now(), now(),'" + in_r_staff_id + "','" + handlegroup_id + "',null)";
                        }
                        log.debug("判断处理工单状态进行下一步处理SQL:" + data1.toString());
                        int[] dataresult1 = eliteBean.updateBatch(data1, "dataSource");

                        sendSMStoStaff(eliteBean, handlegroup_id, in_r_staff_id, customer_guid, workorderid, map_id, in_s_objetive_guid, "SEND", y_createdby, y_createdgroup, sub_doctype);
                        sendSMStoAPP(eliteBean, handlegroup_id, in_r_staff_id, customer_guid, workorderid, map_id, in_s_objetive_guid, "APPSEND", y_createdby, y_createdgroup, sub_doctype);
                    }
                }
            }
        }
        result.put("code", "1");
        //result.put("msg", "下送成功")
        return result;

    }

    //工单结案
    public JSONObject CloseOrder(JdbcTemplate jt, JSONObject tabcValue, EliteBean eliteBean) {
        Map fieldValue = new HashMap<String, Object>();
        JSONObject result = new JSONObject();

        String in_s_objetive_guid = tabcValue.getJSONObject("data").getString("in_s_objetive_guid"); //当前objective_guid
        String in_r_group_id = tabcValue.getJSONObject("data").getString("in_r_group_id"); //下送组
        String in_r_staff_id = tabcValue.getJSONObject("data").getString("in_r_staff_id"); //下送人
        String in_workorderflowdetail = tabcValue.getJSONObject("data").getString("in_workorderflowdetail"); // 工单处理内容描述
        String map_id = tabcValue.getString("map_id"); //工单类型id
        String frame_id = tabcValue.getString("frame_id"); //工单模板id
        String step_id = tabcValue.getString("step_id"); //工单步骤id

        String y_createdby = tabcValue.getString("c_user_id"); //工单创建人
        String y_createdgroup = tabcValue.getString("c_group_id"); //创建组
        String sub_doctype = tabcValue.getString("sub_doctype"); //发送来源(页面ID)

        String y_customerproblem = in_workorderflowdetail; //客户问题
        String tabcname = tabcValue.getString("tabcname");
        String customer_guid = tabcValue.getJSONObject("data").getString("customer_guid"); //当前customer_guid
        String workorderid = tabcValue.getJSONObject("data").getString("workorderid"); //工单流水ID

        JSONObject tabcJSON = tabcValue.getJSONObject("data").getJSONObject("business_info");//获取tabc字段信息
        fieldValue.putAll(tabcJSON);
        String fieldName = "";
        String valueString = "";
        String updateString = "";
        List<Object> valueList = new ArrayList<Object>();
        for (String key : fieldValue.keySet()) {
            if (fieldName == "") {
                fieldName = key;
                valueString = "?";
                updateString = key + "=" + "?";
                insertString = key;
            } else {
                fieldName = fieldName + "," + key;
                valueString = valueString + ",?";
                updateString = updateString + "," + key + "=" + "?";
                insertString = insertString + "," + key;
            }
            //valueList.add(fieldValue.get(key));
            if (!String.valueOf(fieldValue.get(key)).equals("null")) {
                valueList.add(KHUtil.isNull(String.valueOf(fieldValue.get(key))) ? null : fieldValue.get(key));
            } else {
                valueList.add(fieldValue.get(key));
            }
        }
        if (fieldName == "") {
            result.put("code", "-1");
            result.put("msg", "字段不能为空")
            return result;
        }

        String[] data1 = new String[3];
        eliteBean.update("insert into " + tabcname + "_his (hisguid," + insertString + ") select uuid()," + insertString + " from " + tabcname + " t where RELATION_ID='" + in_s_objetive_guid + "'", "dataSource");
        valueList.add(in_s_objetive_guid);

        String tabcsql = "update " + tabcname + " set " + updateString + " where relation_id=?";
        int tabcResule = jt.update(tabcsql, valueList.toArray());
        if (tabcResule > 0) {
            //data1[0] = "insert into tabcwo_business_info_his select uuid(),t.* from tabcwo_business_info t where RELATION_ID='" + in_s_objetive_guid + "'";
            data1[0] = "Update Objective Set comments='" + in_workorderflowdetail + "',Modifiedby = '" + y_createdby + "', Modifieddate = now(),objectivestatus='CLOSE' Where Objective_Guid = '" + in_s_objetive_guid + "'";
            data1[1] = "delete from Elitetask where Objective_Guid = '" + in_s_objetive_guid + "'";
            data1[2] = "Insert Into Workorderstep3 (Workorder_Guid, Workorderstep_Guid, oldHandleby_Id, oldHandlegroup_Id, Workorderstepstatus, Comments, Modifieddate, From_Stepidmap, Workstarttime, Workendtime) Values ('" + in_s_objetive_guid + "', uuid(), '" + y_createdby + "', '" + y_createdgroup + "', 'END', '" + in_workorderflowdetail + "', now(), '" + step_id + "', now(), now())";

            log.debug("判断处理工单状态进行下一步处理SQL:" + data1.toString());
            int[] dataresult1 = eliteBean.updateBatch(data1, "dataSource");

            sendSMStoStaff(eliteBean, '', in_r_staff_id, customer_guid, workorderid, map_id, in_s_objetive_guid, "END", y_createdby, y_createdgroup, sub_doctype);
            sendSMStoCustomer(eliteBean, customer_guid, workorderid, map_id, in_s_objetive_guid, "END", y_createdby, y_createdgroup, sub_doctype);

            result.put("code", "1");
            //result.put("msg", "结案成功")
            return result;
        }

    }

    //工单退回(非正常版本)
    public JSONObject BackOrder(JdbcTemplate jt, JSONObject tabcValue, EliteBean eliteBean) {
        Map fieldValue = new HashMap<String, Object>();
        JSONObject result = new JSONObject();

        String in_s_objetive_guid = tabcValue.getJSONObject("data").getString("in_s_objetive_guid"); //当前objective_guid
        String in_workorderflowdetail = tabcValue.getJSONObject("data").getString("in_workorderflowdetail"); // 工单处理内容描述
        String map_id = tabcValue.getString("map_id"); //工单类型id
        String back_nodemap = tabcValue.getString("back_nodemap"); //工单回退节点
        String frame_id = tabcValue.getString("frame_id"); //工单模板id
        String step_id = tabcValue.getString("step_id"); //工单步骤id
        String customer_guid = tabcValue.getJSONObject("data").getString("customer_guid"); //当前customer_guid
        String y_createdby = tabcValue.getString("c_user_id"); //工单创建人
        String y_createdgroup = tabcValue.getString("c_group_id"); //创建组
        String y_customerproblem = in_workorderflowdetail; //客户问题
        String workorderid = tabcValue.getJSONObject("data").getString("workorderid"); //工单流水ID
        String sub_doctype = tabcValue.getString("sub_doctype"); //发送来源(页面ID)
        String tabcname = tabcValue.getString("tabcname");
        JSONObject tabcJSON = tabcValue.getJSONObject("data").getJSONObject("business_info"); //获取tabc字段信息

        log.debug("map_id:" + map_id);
        log.debug("back_nodemap:" + back_nodemap);

        String back_body = tabcValue.getJSONObject("data").getString("back_body"); // 失败清除的意见
        fieldValue.putAll(tabcJSON);
        String fieldName = "";
        String valueString = "";
        String updateString = ""
        List<Object> valueList = new ArrayList<Object>();
        for (String key : fieldValue.keySet()) {
            if (fieldName == "") {
                fieldName = key;
                valueString = "?";
                updateString = key + "=" + "?";

                insertString = key;
            } else {
                fieldName = fieldName + "," + key;
                valueString = valueString + ",?";
                updateString = updateString + "," + key + "=" + "?";

                insertString = insertString + "," + key;
            }
            //valueList.add(fieldValue.get(key));
            if (!String.valueOf(fieldValue.get(key)).equals("null")) {
                valueList.add(KHUtil.isNull(String.valueOf(fieldValue.get(key))) ? null : fieldValue.get(key));
            } else {
                valueList.add(fieldValue.get(key));
            }
        }
        valueList.add(in_s_objetive_guid);
        if (fieldName == "") {
            result.put("code", "-1");
            result.put("msg", "字段不能为空")
            return result;
        }

        String[] data1 = new String[6];
        eliteBean.update("insert into " + tabcname + "_his (hisguid," + insertString + ") select uuid()," + insertString + " from " + tabcname + " t where RELATION_ID='" + in_s_objetive_guid + "'", "dataSource");
        String tabcsql = "update " + tabcname + " set " + updateString + " where relation_id=?";
        log.debug("tabcsql2" + tabcsql);
        int tabcResule = jt.update(tabcsql, valueList.toArray());
        if (tabcResule > 0) {
            //工单类型为：租赁申请表（一年以下）、租赁申请表（一年以上）广告租赁 的退回至第一个节点
            if (map_id == '3H17Z4' || map_id == 'SNBAUZ' || map_id == 'X88KSL') {
                String createSql = "select oldhandleby_id,oldhandlegroup_id,from_stepidmap from workorderstep3 where workorder_guid='" + in_s_objetive_guid + "' order by workstarttime limit 1"; //查询第一步的处理人和步骤id
                List<Map<String, Object>> createList = jt.queryForList(createSql);
                log.debug("");
                String oldhandleby_id = createList.get(0).get("oldhandleby_id");
                String oldhandlerolegroup_id = createList.get(0).get("oldhandlegroup_id");
                String from_stepidmap = createList.get(0).get("from_stepidmap");

                //data1[0] = "insert into tabcwo_business_info_his select uuid(),t.* from tabcwo_business_info t where RELATION_ID='" + in_s_objetive_guid + "'";
                data1[0] = "Update Objective Set comments='" + in_workorderflowdetail + "',Modifiedby = '" + y_createdby + "', Modifieddate = now(),objectivestatus='OPEN',handlerole_id=null,handleby_id='" + oldhandleby_id + "',handlegroup_id='" + oldhandlerolegroup_id + "',Curstep_Id_Map='" + from_stepidmap + "' Where Objective_Guid = '" + in_s_objetive_guid + "'";
                data1[1] = "delete from Elitetask where Objective_Guid = '" + in_s_objetive_guid + "'";
                data1[2] = "Insert Into Elitetask (Elitetask_Guid, Objective_Guid, Objectivetype_Id, Expectstarttime, Expectendtime, Priority, Handleby_Id, Handlegroup_Id, Createdby, Createddate, Modifiedby, Modifieddate, Comments, Customer_Guid, Customerlevel, Localtrycount, Curstepstatus, Curstep_Id, Stepforecastdate, Curstep_Id_Map, Map_Id, Objectivesubstatus, Elitetaskstatus, Obflag) Values (uuid(), '" + in_s_objetive_guid + "', '" + frame_id + "', now(), now(), 'NORMAL', '" + oldhandleby_id + "', '" + oldhandlerolegroup_id + "', '" + y_createdby + "', now(), '" + y_createdby + "', now(), '" + in_workorderflowdetail + "', '" + customer_guid + "', Null, '0', 'OPEN ', '" + from_stepidmap + "', now(), '" + from_stepidmap + "', '" + map_id + "', 'OPEN', Null, '0')";
                data1[3] = "Insert Into Workorderstep3 (Workorder_Guid, Workorderstep_Guid, oldHandleby_Id, oldHandlegroup_Id, Workorderstepstatus, Comments, Modifieddate, From_Stepidmap,to_stepidmap, Workstarttime, Workendtime,handleby_id,handlegroup_id) Values ('" + in_s_objetive_guid + "', uuid(), '" + y_createdby + "', '" + y_createdgroup + "', 'BACK', '" + in_workorderflowdetail + "', now(), '" + step_id + "','" + from_stepidmap + "', now(), now(),'" + oldhandleby_id + "','" + oldhandlerolegroup_id + "')";
                data1[4] = "insert into workorderstep3_his select * from Workorderstep3 where Workorder_Guid='" + in_s_objetive_guid + "'";
                //data1[5] = "delete from Workorderstep3 where Workorder_Guid='" + in_s_objetive_guid + "'";

                log.debug("判断处理工单状态进行下一步处理SQL:" + data1.toString());
                int[] dataresult1 = eliteBean.updateBatch(data1, "dataSource");

                sendSMStoStaff(eliteBean, '', '', customer_guid, workorderid, map_id, in_s_objetive_guid, "BACK_ALL", y_createdby, y_createdgroup, sub_doctype);
                sendSMStoAPP(eliteBean, '', '', customer_guid, workorderid, map_id, in_s_objetive_guid, "BACK_ALL", y_createdby, y_createdgroup, sub_doctype);

                result.put("code", "1");
                return result;
            } else {
                String createSql = "select oldhandleby_id,oldhandlegroup_id,from_stepidmap from workorderstep3 where workorder_guid='" + in_s_objetive_guid + "' and from_stepidmap='" + back_nodemap + "'order by modifieddate desc limit 1"; //查询回退节点的处理人和组
                List<Map<String, Object>> createList = jt.queryForList(createSql);
                log.debug("");
                String oldhandleby_id = createList.get(0).get("oldhandleby_id");
                String oldhandlerolegroup_id = createList.get(0).get("oldhandlegroup_id");

                data1[0] = "Update Objective Set comments='" + in_workorderflowdetail + "',Modifiedby = '" + y_createdby + "', Modifieddate = now(), objectivestatus='OPEN', handlerole_id=null, handleby_id='" + oldhandleby_id + "', handlegroup_id='" + oldhandlerolegroup_id + "',Curstep_Id_Map='" + back_nodemap + "' Where Objective_Guid = '" + in_s_objetive_guid + "'";
                data1[1] = "delete from Elitetask where Objective_Guid = '" + in_s_objetive_guid + "'";
                data1[2] = "Insert Into Elitetask (Elitetask_Guid, Objective_Guid, Objectivetype_Id, Expectstarttime, Expectendtime, Priority, Handleby_Id, Handlegroup_Id, Createdby, Createddate, Modifiedby, Modifieddate, Comments, Customer_Guid, Customerlevel, Localtrycount, Curstepstatus, Curstep_Id, Stepforecastdate, Curstep_Id_Map, Map_Id, Objectivesubstatus, Elitetaskstatus, Obflag) Values (uuid(), '" + in_s_objetive_guid + "', '" + frame_id + "', now(), now(), 'NORMAL', '" + oldhandleby_id + "', '" + oldhandlerolegroup_id + "', '" + y_createdby + "', now(), '" + y_createdby + "', now(), '" + in_workorderflowdetail + "', '" + customer_guid + "', Null, '0', 'OPEN ', '" + back_nodemap + "', now(), '" + back_nodemap + "', '" + map_id + "', 'OPEN', Null, '0')";
                data1[3] = "Insert Into Workorderstep3 (Workorder_Guid, Workorderstep_Guid, oldHandleby_Id, oldHandlegroup_Id, Workorderstepstatus, Comments, Modifieddate, From_Stepidmap,to_stepidmap, Workstarttime, Workendtime,handleby_id,handlegroup_id) Values ('" + in_s_objetive_guid + "', uuid(), '" + y_createdby + "', '" + y_createdgroup + "', 'BACK', '" + in_workorderflowdetail + "', now(), '" + step_id + "','" + back_nodemap + "', now(), now(),'" + oldhandleby_id + "','" + oldhandlerolegroup_id + "')";
                data1[4] = "insert into workorderstep3_his select * from Workorderstep3 where Workorder_Guid='" + in_s_objetive_guid + "'order by Modifieddate desc limit 1";

                log.debug("判断处理工单状态进行下一步处理SQL:" + data1.toString());
                int[] dataresult1 = eliteBean.updateBatch(data1, "dataSource");

                sendSMStoStaff(eliteBean, '', '', customer_guid, workorderid, map_id, in_s_objetive_guid, "BACK_ALL", y_createdby, y_createdgroup, sub_doctype);
                sendSMStoAPP(eliteBean, '', '', customer_guid, workorderid, map_id, in_s_objetive_guid, "BACK_ALL", y_createdby, y_createdgroup, sub_doctype);

                result.put("code", "1");
                return result;
            }

        }

        result.put("code", "-1");
        //.put("msg", "退回成功")
        return result;
    }

    //催单
    public JSONObject UrgeOrder(JdbcTemplate jt, JSONObject tabcValue, EliteBean eliteBean) {
        JSONObject result = new JSONObject();

        String in_s_objetive_guid = tabcValue.getJSONObject("data").getString("in_s_objetive_guid"); //当前objective_guid
        String in_workorderflowdetail = tabcValue.getJSONObject("data").getString("in_workorderflowdetail"); // 工单处理内容描述
        String map_id = tabcValue.getString("map_id"); //工单类型id
        String frame_id = tabcValue.getString("frame_id"); //工单模板id
        String step_id = tabcValue.getString("step_id"); //工单步骤id
        String customer_guid = tabcValue.getJSONObject("data").getString("customer_guid"); //当前customer_guid
        String y_createdby = tabcValue.getString("c_user_id"); //工单创建人
        String y_createdgroup = tabcValue.getString("c_group_id"); //创建组
        String y_customerproblem = in_workorderflowdetail; //客户问题

        String[] data1 = new String[2];
        data1[0] = "Update Objective Set Modifiedby = '" + y_createdby + "', Modifieddate = now() Where Objective_Guid = '" + in_s_objetive_guid + "'";
        data1[1] = "Insert Into Workorderstep3 (Workorder_Guid, Workorderstep_Guid, oldHandleby_Id, oldHandlegroup_Id, Workorderstepstatus, Comments, Modifieddate, From_Stepidmap, Workstarttime, Workendtime) Values ('" + in_s_objetive_guid + "', uuid(), '" + y_createdby + "', '" + y_createdgroup + "', 'URGE', '" + in_workorderflowdetail + "', now(), '" + step_id + "', now(), now())";

        log.debug("判断处理工单状态进行下一步处理SQL:" + data1.toString());
        int[] dataresult1 = eliteBean.updateBatch(data1, "dataSource");
        result.put("code", "1");
        //.put("msg", "退回成功")
        return result;
    }

    //正常退回
    public JSONObject BackOrderNormal(JdbcTemplate jt, JSONObject tabcValue, EliteBean eliteBean) {

    }

    //接管
    public JSONObject TakeOrder(JdbcTemplate jt, JSONObject tabcValue, EliteBean eliteBean) {
        JSONObject result = new JSONObject();

        String in_s_objetive_guid = tabcValue.getJSONObject("data").getString("in_s_objetive_guid"); //当前objective_guid
        String in_s_elitetask_guid = tabcValue.getJSONObject("data").getString("in_s_elitetask_guid"); //当前objective_guid
        String in_workorderflowdetail = tabcValue.getJSONObject("data").getString("in_workorderflowdetail"); // 工单处理内容描述
        String map_id = tabcValue.getString("map_id"); //工单类型id
        String frame_id = tabcValue.getString("frame_id"); //工单模板id
        String step_id = tabcValue.getString("step_id"); //工单步骤id
        String customer_guid = tabcValue.getJSONObject("data").getString("customer_guid"); //当前customer_guid
        String y_createdby = tabcValue.getString("c_user_id"); //工单创建人
        String y_createdgroup = tabcValue.getString("c_group_id"); //创建组
        String y_customerproblem = in_workorderflowdetail; //客户问题

        String createSql = "select handleby_id,handlegroup_id from elitetask where elitetask_guid='" + in_s_elitetask_guid + "'"; //查询第一步的处理人和步骤id
        List<Map<String, Object>> createList = jt.queryForList(createSql);
        String oldhandleby_id = createList.get(0).get("handleby_id");
        String oldhandlerolegroup_id = createList.get(0).get("handlegroup_id");

        String[] data1 = new String[3];
        data1[0] = "Update Objective Set Modifiedby = '" + y_createdby + "', Modifieddate = now(),handleby_id='" + y_createdby + "',handlegroup_id='" + y_createdgroup + "' Where Objective_Guid = '" + in_s_objetive_guid + "'";
        data1[1] = "Update elitetask Set Modifiedby = '" + y_createdby + "', Modifieddate = now(),handleby_id='" + y_createdby + "',handlegroup_id='" + y_createdgroup + "',handlerole_id=null Where elitetask_guid = '" + in_s_elitetask_guid + "'";
        data1[2] = "Insert Into Workorderstep3 (Workorder_Guid, Workorderstep_Guid, oldHandleby_Id, oldHandlegroup_Id,handleby_id,handlegroup_id, Workorderstepstatus, Comments, Modifieddate, From_Stepidmap, Workstarttime, Workendtime) Values ('" + in_s_objetive_guid + "', uuid(), '" + oldhandleby_id + "', '" + oldhandlerolegroup_id + "','" + y_createdby + "','" + y_createdgroup + "','TAKE', '" + in_workorderflowdetail + "', now(), '" + step_id + "', now(), now())";

        log.debug("判断处理工单状态进行下一步处理SQL:" + data1.toString());
        int[] dataresult1 = eliteBean.updateBatch(data1, "dataSource");
        result.put("code", "1");
        //.put("msg", "退回成功")
        return result;
    }

    //指派
    public JSONObject ASSIGNOrder(JdbcTemplate jt, JSONObject tabcValue, EliteBean eliteBean) {
        JSONObject result = new JSONObject();

        String in_s_objetive_guid = tabcValue.getJSONObject("data").getString("in_s_objetive_guid"); //当前objective_guid
        String in_s_elitetask_guid = tabcValue.getJSONObject("data").getString("in_s_elitetask_guid"); //当前objective_guid
        String customer_guid = tabcValue.getJSONObject("data").getString("customer_guid"); //当前customer_guid
        String in_workorderflowdetail = tabcValue.getJSONObject("data").getString("in_workorderflowdetail"); // 工单处理内容描述

        String in_r_staff_id = tabcValue.getString("in_r_staff_id"); //指派给的人
        String in_r_group_id = tabcValue.getString("in_r_group_id"); //指派给的组
        String map_id = tabcValue.getString("map_id"); //工单类型id
        String frame_id = tabcValue.getString("frame_id"); //工单模板id
        String step_id = tabcValue.getString("step_id"); //工单步骤id
        String y_createdby = tabcValue.getString("c_user_id"); //工单当前操作人
        String y_createdgroup = tabcValue.getString("c_group_id"); //工单当前操作组

        String createSql = "select handleby_id,handlegroup_id from elitetask where elitetask_guid='" + in_s_elitetask_guid + "'"; //查询的当前步骤的处理人组
        List<Map<String, Object>> createList = jt.queryForList(createSql);
        String oldhandleby_id = createList.get(0).get("handleby_id");
        String oldhandlerolegroup_id = createList.get(0).get("handlegroup_id");

        String[] data1 = new String[3];
        data1[0] = "Update Objective Set Modifiedby = '" + y_createdby + "', Modifieddate = now(), handleby_id='" + in_r_staff_id + "', handlegroup_id='" + in_r_group_id + "' Where Objective_Guid = '" + in_s_objetive_guid + "'";
        data1[1] = "Update elitetask Set Modifiedby = '" + y_createdby + "', Modifieddate = now(), handleby_id='" + in_r_staff_id + "', handlegroup_id='" + in_r_group_id + "', handlerole_id=null Where elitetask_guid = '" + in_s_elitetask_guid + "'";
        data1[2] = "Insert Into Workorderstep3 (Workorder_Guid, Workorderstep_Guid, oldHandleby_Id, oldHandlegroup_Id,handleby_id,handlegroup_id, Workorderstepstatus, Comments, Modifieddate, From_Stepidmap, TO_STEPIDMAP, Workstarttime, Workendtime) " + "Values ('" + in_s_objetive_guid + "', uuid(), '" + oldhandleby_id + "', '" + oldhandlerolegroup_id + "','" + in_r_staff_id + "','" + in_r_group_id + "','ZHIPAI', '" + in_workorderflowdetail + "', now(), '" + step_id + "', '" + step_id + "', now(), now())";

        log.debug("判断处理工单状态进行下一步处理SQL:" + data1.toString());
        int[] dataresult1 = eliteBean.updateBatch(data1, "dataSource");

        result.put("code", "1");
        //.put("msg", "指派成功")
        return result;
    }

    //发送处理短信到坐席
    public void sendSMStoStaff(EliteBean eliteBean, String rolegroup_id, String staff_id, String customer_guid, String workorderid, String mapid, String objective_guid, String type, String c_user_id, String c_group_id, String sub_doctype) {
        log.debug("发送处理短信到坐席");
        String mobile = "";
        if (staff_id == "") {
            String mobilesql = "select s.mobile from staffrolegroup sr left join staff s on s.staff_id=sr.staff_id where sr.rolegroup_id='" + rolegroup_id + "' and s.mobile is not null and s.CHECK_ALL_PROCESS=1"
            def mobilelist = eliteBean.chaxun_sql(mobilesql, "dataSource");
            if (mobilelist) {
                mobile = mobilelist[0].get("mobile");
            } else {
                return;
            }
        } else {
            String mobilesql = "select s.mobile from staff s where staff_id='" + staff_id + "'";
            def mobilelist = eliteBean.chaxun_sql(mobilesql, "dataSource");
            if (mobilelist) {
                mobile = mobilelist[0].get("mobile");
            } else {
                return;
            }
        }

        String modelStr = "";
        if (type == "SEND") {
            String modelSql = "select send_message from send_message_template where map_id='" + mapid + "' and SEND_TYPE='Staff'";
            def modelList = eliteBean.chaxun_sql(modelSql, "dataSource");
            modelStr = modelList[0].get("send_message").toString();
            log.debug("modelStr:" + modelStr);
            log.debug("mobile:" + mobile);
            if (modelStr != "" && mobile != "") {
                modelStr = modelStr.replace("{workorderno}", workorderid);
                String documentsql = "insert into Documentout (DOCUMENTOUT_GUID,DOCUMENTPATH,DOCUMENTTO,expectedstartdate,Status,Trialcount,Trialcountlimit,DOCTYPE,CUSTOMER_GUID,OBJECTIVE_GUID,CREATEDDATE,HANDLEBY_ID,HANDLEGROUP_ID,SUB_DOCTYPE) values (uuid(),'" + modelStr + "','" + mobile + "',NOW(),3,0,5,3,'" + customer_guid + "','" + objective_guid + "',NOW(),'" + c_user_id + "','" + c_group_id + "','" + sub_doctype + "')";
                eliteBean.update(documentsql, "dataSource");
            }
        } else if (type == "END") {
            String modelSql = "select send_message from send_message_template where map_id='" + mapid + "' and SEND_TYPE='all'";
            def modelList = eliteBean.chaxun_sql(modelSql, "dataSource");
            modelStr = modelList[0].get("send_message").toString();
            modelStr = modelStr.replace("{workorderno}", workorderid);
            String stepString = " select distinct s.mobile from workorderstep3 ws left join staff s on s.staff_id=ws.OLDHANDLEBY_ID where workorder_guid='" + objective_guid + "'  and (workorderstepstatus='SENDNS' or workorderstepstatus='END') and mobile is not null";
            def mobilelist = eliteBean.chaxun_sql(modelSql, "dataSource");
            for (def i = 0; i < modelList.size(); i++) {
                mobile = mobilelist[i].get("mobile");
                String documentsql = "insert into Documentout (DOCUMENTOUT_GUID,DOCUMENTPATH,DOCUMENTTO,expectedstartdate,Status,Trialcount,Trialcountlimit,DOCTYPE,CUSTOMER_GUID,OBJECTIVE_GUID,CREATEDDATE,HANDLEBY_ID,HANDLEGROUP_ID,SUB_DOCTYPE) values (uuid(),'" + modelStr + "','" + mobile + "',NOW(),3,0,5,3,'" + customer_guid + "','" + objective_guid + "',NOW(),'" + c_user_id + "','" + c_group_id + "','" + sub_doctype + "')";
                eliteBean.update(documentsql, "dataSource");
            }
        } else if (type == "BACK_ALL") {
            String modelSql = "select send_message from send_message_template where map_id='" + mapid + "' and SEND_TYPE='Back_All'";
            def modelList = eliteBean.chaxun_sql(modelSql, "dataSource");
            modelStr = modelList[0].get("send_message").toString();
            modelStr = modelStr.replace("{workorderno}", workorderid);
            String stepString = " select distinct s.mobile from workorderstep3 ws left join staff s on s.staff_id=ws.OLDHANDLEBY_ID where workorder_guid='" + objective_guid + "'  and (workorderstepstatus='SENDNS' or workorderstepstatus='BACK') and mobile is not null";
            def steplist = eliteBean.chaxun_sql(stepString, "dataSource");
            for (def i = 0; i < steplist.size(); i++) {
                mobile = steplist[i].get("mobile");
                String documentsql = "insert into Documentout (DOCUMENTOUT_GUID,DOCUMENTPATH,DOCUMENTTO,expectedstartdate,Status,Trialcount,Trialcountlimit,DOCTYPE,CUSTOMER_GUID,OBJECTIVE_GUID,CREATEDDATE,HANDLEBY_ID,HANDLEGROUP_ID,SUB_DOCTYPE) values (uuid(),'" + modelStr + "','" + mobile + "',NOW(),3,0,5,3,'" + customer_guid + "','" + objective_guid + "',NOW(),'" + c_user_id + "','" + c_group_id + "','" + sub_doctype + "')";
                eliteBean.update(documentsql, "dataSource");
            }
        }
    }

    //发送处理信息到APP
    public void sendSMStoAPP(EliteBean eliteBean, String rolegroup_id, String staff_id, String customer_guid, String workorderid, String mapid, String objective_guid, String type, String c_user_id, String c_group_id, String sub_doctype) {
        log.debug("发送处理信息到APP");
        String loginname = "";
        if (staff_id == "") {
            String loginnamesql = "select s.loginname from staffrolegroup sr left join staff s on s.staff_id=sr.staff_id where sr.rolegroup_id='" + rolegroup_id + "' and s.mobile is not null and s.CHECK_ALL_PROCESS=1"
            def loginnamelist = eliteBean.chaxun_sql(loginnamesql, "dataSource");
            if (loginnamelist) {
                loginname = loginnamelist[0].get("loginname");
            } else {
                return;
            }
        } else {
            String loginnamesql = "select s.loginname from staff s where staff_id='" + staff_id + "'";
            def loginnamelist = eliteBean.chaxun_sql(loginnamesql, "dataSource");
            if (loginnamelist) {
                loginname = loginnamelist[0].get("loginname");
            } else {
                return;
            }
        }

        String modelStr = "";
        if (type == "APPSEND") {
            String modelSql = "select send_message from send_message_template where map_id='" + mapid + "' and SEND_TYPE='Staff'";
            def modelList = eliteBean.chaxun_sql(modelSql, "dataSource");
            modelStr = modelList[0].get("send_message").toString();
            log.debug("sendSMStoAPP_modelStr:" + modelStr);
            if (modelStr != "") {
                modelStr = modelStr.replace("{workorderno}", workorderid);
                String documentsql = "insert into Documentout (DOCUMENTOUT_GUID,DOCUMENTPATH,DOCUMENTTO,expectedstartdate,Status,Trialcount,Trialcountlimit,DOCTYPE,CUSTOMER_GUID,OBJECTIVE_GUID,CREATEDDATE,HANDLEBY_ID,HANDLEGROUP_ID,SUB_DOCTYPE) values (uuid(),'" + modelStr + "','" + loginname + "',NOW(),3,0,5,6,'" + customer_guid + "','" + objective_guid + "',NOW(),'" + c_user_id + "','" + c_group_id + "','" + sub_doctype + "')";
                eliteBean.update(documentsql, "dataSource");
            }
        } else if (type == "BACK_ALL") {
            log.debug("发送回退信息到APP给所有坐席");
            String modelSql = "select send_message from send_message_template where map_id='" + mapid + "' and SEND_TYPE='Back_All'";
            def modelList = eliteBean.chaxun_sql(modelSql, "dataSource");
            modelStr = modelList[0].get("send_message").toString();
            modelStr = modelStr.replace("{workorderno}", workorderid);
            String stepString = '''Select Distinct s.Loginname, Ws.Oldhandlegroup_Id, Ws.Oldhandleby_Id, Et.Elitetask_Guid, Fm.Mapname
                                      From Workorderstep3 Ws
                                      Left Join Staff s
                                        On s.Staff_Id = Ws.Oldhandleby_Id
                                      Left Join Elitetask Et
                                        On Ws.Workorder_Guid = Et.Objective_Guid
                                      Left Join Wo3_Frame_Map Fm
                                        On Et.Map_Id = Fm.Map_Id
                                     Where Ws.Workorder_Guid = \'''' + objective_guid + '''\'
                                       And (Workorderstepstatus = \'SENDNS\' Or Workorderstepstatus = \'BACK\')
                                       And Mobile Is Not Null''';
            def steplist = eliteBean.chaxun_sql(stepString, "dataSource");
            for (def i = 0; i < steplist.size(); i++) {
                loginname = steplist[i].get("Loginname");

                String NoticeGroup = steplist[i].get("Oldhandlegroup_Id");
                String NoticeBy = steplist[i].get("Oldhandleby_Id");
                String Elitetask_Guid = steplist[i].get("Elitetask_Guid");
                String Mapname = steplist[i].get("Mapname");

                String documentsql = "insert into Documentout (DOCUMENTOUT_GUID,DOCUMENTPATH,DOCUMENTTO,expectedstartdate,Status,Trialcount,Trialcountlimit,DOCTYPE,CUSTOMER_GUID,OBJECTIVE_GUID,CREATEDDATE,HANDLEBY_ID,HANDLEGROUP_ID,SUB_DOCTYPE) values (uuid(),'" + modelStr + "','" + loginname + "',NOW(),3,0,5,6,'" + customer_guid + "','" + objective_guid + "',NOW(),'" + c_user_id + "','" + c_group_id + "','" + sub_doctype + "')";
                eliteBean.update(documentsql, "dataSource");

                JSONObject noticeData = new JSONObject();
                noticeData.put("addinId", "AddinWO3");
                noticeData.put("tabName", Mapname);
                noticeData.put("oId", objective_guid);
                noticeData.put("taskId", Elitetask_Guid);

                String notictSQL = '''Insert Into Kh_Pc_Notice
  (Noticeid, Noticetype, Noticegroup, Noticeby, Creategroup, Createby, Noticecontent, noticeData, Createddate, Limitcount)
Values
  (Uuid(), '1', \'''' + NoticeGroup + '''\', \'''' + NoticeBy + '''\', \'''' + c_group_id + '''\', \'''' + c_user_id + '''\', \'''' + modelStr + '''\', \'''' + noticeData.toString() + '''\', Now(), 1)''';
                eliteBean.update(notictSQL, "dataSource");
            }
        }
    }

    //发送短信到客户
    public void sendSMStoCustomer(EliteBean eliteBean, String customer_guid, String workorderid, String mapid, String objective_guid, String type, String c_user_id, String c_group_id, String sub_doctype) {
        log.debug("发送短信到客户");
        String mobile = "";
        String open_id = "";
        String mobilesql = "select s.mobile,open_id from customer s where  customer_guid='" + customer_guid + "'";
        def mobilelist = eliteBean.chaxun_sql(mobilesql, "dataSource");
        mobile = mobilelist[0].get("mobile");
        open_id = mobilelist[0].get("open_id");
        String modelStr;
        if (type == "Create") {
            String modelSql = "select send_message from send_message_template where map_id='" + mapid + "' and SEND_TYPE='Cust_start'";
            def modelList = eliteBean.chaxun_sql(modelSql, "dataSource");
            modelStr = modelList[0].get("send_message").toString();
        } else if (type == "END") {
            String modelSql = "select send_message from send_message_template where map_id='" + mapid + "' and SEND_TYPE='all'";
            def modelList = eliteBean.chaxun_sql(modelSql, "dataSource");
            modelStr = modelList[0].get("send_message").toString();
        }
        log.debug("modelStr:" + modelStr);
        modelStr = modelStr.replace("{workorderno}", workorderid);
        if (!open_id) {
            if (modelStr != "" && mobile != "") {
                String documentsql = "insert into Documentout (DOCUMENTOUT_GUID,DOCUMENTPATH,DOCUMENTTO,expectedstartdate,Status,Trialcount,Trialcountlimit,DOCTYPE,CUSTOMER_GUID,OBJECTIVE_GUID,CREATEDDATE,HANDLEBY_ID,HANDLEGROUP_ID,SUB_DOCTYPE) values (uuid(),'" + modelStr + "','" + mobile + "',NOW(),3,0,5,3,'" + customer_guid + "','" + objective_guid + "',NOW(),'" + c_user_id + "','" + c_group_id + "','" + sub_doctype + "')";
                eliteBean.update(documentsql, "dataSource");
            }
        } else {
            HashMap<String, Object> headMap = new HashMap<>();
            headMap.put("Content-Type", "application/json");
            Long timestamp = System.currentTimeMillis(); // 当前时间戳
            net.sf.json.JSONObject date_1 = new net.sf.json.JSONObject();
            date_1.put("access_key", "123213123ACCESS_KEY");
            date_1.put("nonce", "random");
            date_1.put("timestamp", timestamp);
            date_1.put("sign", "B9670B476F2CA493888448DD1E4BE846");

            net.sf.json.JSONObject post_body = new net.sf.json.JSONObject();
            post_body.put("send_type", "受理进度");
            post_body.put("send_target", "customer");
            post_body.put("workorder_guid", objective_guid);
            post_body.put("user_id", "");
            post_body.put("send_remark", modelStr);
            post_body.put("send_url", "受理进度");

            date_1.put("data", post_body);

            String SendSql = " select target_url from sys_interface_config where type = 'apiWOSendNotice'";
            def SendList = eliteBean.chaxun_sql(SendSql, "dataSource");
            String SendUrl = SendList[0].get("target_url");
            String Assemblys = Util.doHttpPostUseHead(SendUrl, date_1.toString().replace("\\n", ""), charset, timeout, enableSSLflag, headMap);
        }

    }

    //测试换行
    public void lineTest(JSONObject tabcValue, EliteBean eliteBean) {
        String in_s_objetive_guid = tabcValue.getJSONObject("data").getString("in_s_objetive_guid"); //当前objective_guid
        String wy_administrationopinion = tabcValue.getJSONObject("data").getJSONObject("business_info").getString("wy_administrationopinion"); //获取tabc字段信息
        String tabcsql = "update tabcwo_renovation set wy_administrationopinion='" + wy_administrationopinion + "' where relation_id='" + in_s_objetive_guid + "'";
        eliteBean.update(tabcsql, "dataSource")
        log.debug(tabcValue);
    }

    // 处理JSON字符串中的特殊字符
    public static String filterJson(String inData) {
        List<String> filterList = new ArrayList<>();
        filterList.add("\\\\");
        filterList.add("¥");
        filterList.add("@");
        filterList.add("\b");
        filterList.add("\f");
        filterList.add("\n");
        filterList.add("\n");
        filterList.add("\t");
        for (int i = 0; i < filterList.size(); i++) {
            inData = inData.replaceAll(filterList.get(i), "");
        }
        inData = inData.replaceAll("'", "''");
        inData = toJsonString(inData);
        return inData;
    }

    // 处理json字符串中value多余的双引号， 将多余的双引号替换为中文双引号
    private static String toJsonString(String s) {
        char[] tempArr = s.toCharArray();
        int tempLength = tempArr.length;
        for (int i = 0; i < tempLength; i++) {
            if (tempArr[i] == ':' && tempArr[i + 1] == '"') {
                for (int j = i + 2; j < tempLength; j++) {
                    if (tempArr[j] == '"') {
                        if (tempArr[j + 1] != ',' && tempArr[j + 1] != '}') {
                            tempArr[j] = '”'; // 将value中的 双引号替换为中文双引号
                        } else if (tempArr[j + 1] == ',' || tempArr[j + 1] == '}') {
                            break;
                        }
                    }
                }
            }
        }
        return new String(tempArr);
    }
}